const config = require("../../utils/config");

Page({
  data: {
    userInfo: {},
    timer: null,
  },

  onLoad() {
    this.loadUserData();
  },

  // 新增生命周期函数
  onShow() {
    // 页面每次显示时重新加载数据
    console.log("调用刷新用户信息的方法")
    // 防抖：500ms 内仅允许触发一次
    if (this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      this.loadUserData();
    }, 500);
  },

  onTabItemTap() {
    console.log("Tab 被点击，刷新数据");
    this.loadUserData();
  },

  async loadUserData() {
    // wx.showLoading({ title: '加载中' });
    try {
      const userInfo = await wx.getStorageSync("userInfo");
      userInfo.avatarUrl = config.ImageURL + userInfo.avatarUrl;
      this.setData({ userInfo: userInfo || {} });
    } catch (err) {
      wx.showToast({ title: "加载失败，请重试", icon: "none" });
    }
    // } finally {
    //   wx.hideLoading();
    // }
  },

  // 编辑个人信息
  editProfile() {
    wx.navigateTo({
      url: "/pages/profile/edit/edit",
    });
  },
  
  // 页面跳转
  navigateToFeedback() {
    wx.navigateTo({
      url: "/pages/myFeedback/myFeedback",
    });
  },

  navigateToHistory() {
    wx.navigateTo({
      url: "/pages/history/history",
    });
  },

  navigateToRecommend() {
    wx.navigateTo({
      url: "/pages/recommend/recommend",
    });
  },

  navigateToSettings() {
    wx.navigateTo({
      url: "/pages/settings/settings",
    });
  },

  navigateToHelp() {
    wx.navigateTo({
      url: "/pages/help/help",
    });
  },

  navigateToAbout() {
    wx.navigateTo({
      url: "/pages/about/about",
    });
  },
});
